﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Common.Lib.Dto
{
    /// <summary>
    /// 基础Dto
    /// </summary>
    public class BaseDto : INotifyPropertyChanged
    {
        #region Id

        /// <summary>
        /// Id
        /// </summary>
        private int? id;

        /// <summary>
        /// Id
        /// </summary>
		public int? Id
        {
            get { return id; }
            set
            {
                id = value;
                OnPropertyChanged();
            }
        }

        #endregion Id 

        #region 创建时间

        /// <summary>
        /// 创建时间
        /// </summary>
        private DateTime? createTime;

        /// <summary>
        /// 创建时间
        /// </summary>
		public DateTime? CreateTime
        {
            get { return createTime; }
            set
            {
                createTime = value;
                OnPropertyChanged();
            }
        }

        #endregion 创建时间 

        #region 更新时间

        /// <summary>
        /// 更新时间
        /// </summary>
        private DateTime? updateTime;

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? UpdateTime
        {
            get { return updateTime; }
            set
            {
                updateTime = value;
                OnPropertyChanged();
            }
        }

        #endregion 更新时间 


        public event PropertyChangedEventHandler? PropertyChanged;

        /// <summary>
        /// 实现通知更新
        /// </summary>
        /// <param name="propertyName"></param>
        public void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            if (!string.IsNullOrEmpty(propertyName))
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = "")
        {
            if (EqualityComparer<T>.Default.Equals(storage, value))
            {
                return false;
            }

            storage = value;
            OnPropertyChanged(propertyName);
            return true;
        }

        protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = "")
        {
            if (EqualityComparer<T>.Default.Equals(storage, value))
            {
                return false;
            }

            storage = value;
            onChanged?.Invoke();
            OnPropertyChanged(propertyName);
            return true;
        }
    }
}
